import pygame
from pygame.locals import *
import time,random

class Heroplane:
	def __init__(self,screen_temp):
		self.x=200
		self.y=400
		self.image=pygame.image.load("./images/me.png")
		self.screen=screen_temp
		self.bullet_list=[]

	def move_left(self):
		self.x-=5
		if self.x<=0:
			self.x=0

	def move_right(self):
		self.x+=5
		if self.x>=408:
			self.x=408

	def fire(self):
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))
		print(len(self.bullet_list))


	def display(self):
		self.screen.blit(self.image,(self.x,self.y))
		for b in self.bullet_list:
			b.display()
			if b.move():
				self.bullet_list.remove(b)

	def check(self,enemy_temp):
		for bo in enemy_temp.bullet_list:
			if bo.x>=self.x+12 and bo.x<self.x+94 and bo.y>=self.y+20 and bo.y<self.y+60:
				print("you lose!")
				exit()


class Bullet:
	def __init__(self,screen_temp,x,y):
		self.x=x+53
		self.y=y
		self.screen=screen_temp
		self.image=pygame.image.load("./images/pd.png")

	def move(self):
		self.y-=10
		if self.y<=-20:
			return True

	def display(self):
		self.screen.blit(self.image,(self.x,self.y))



class EnemyPlane:
	def __init__(self,screen_temp):
		self.x=random.choice(range(408))
		self.y=-75
		self.image=pygame.image.load("./images/e2.png")
		self.screen=screen_temp
		self.bullet_list=[]
		

	def display(self):
		self.screen.blit(self.image,(self.x,self.y))
		for b in self.bullet_list:
			b.display()
			if b.move():
				self.bullet_list.remove(b)

	def fire(self):
		self.bullet_list.append(Bullet2(self.screen,self.x,self.y))
		print(len(self.bullet_list))

	def move(self,hero):
		self.y+=3
		if self.y>=568:
			return True

		for bo in hero.bullet_list:
			if bo.x>=self.x+12 and bo.x<self.x+94 and bo.y>=self.y+20 and bo.y<self.y+60:
				
				hero.bullet_list.remove(bo)
				return True


class Bullet2:
	def __init__(self,screen_temp,x,y):
		self.x=x+52
		self.y=y+74
		self.screen=screen_temp
		self.image=pygame.image.load("./images/pd.png")

	def move(self):
		self.y+=5
		if self.y>=568:
			return True

	def display(self):
		self.screen.blit(self.image,(self.x,self.y))

class bombimage:
	def __init__(self,screen_temp,x,y):
		self.x=x
		self.y=y
		self.screen=screen_temp
		self.bombimage1=pygame.image.load("./images/bomb1.png")
		self.bombimage2=pygame.image.load("./images/bomb2.png")
		self.bombimage3=pygame.image.load("./images/bomb3.png")
		self.bombimage4=pygame.image.load("./images/bomb4.png")

	def bombdisplay(self):
		self.screen.blit(self.bombimage1,(self.x+42,self.y+27))
		self.screen.blit(self.bombimage2,(self.x+36,self.y+21))
		self.screen.blit(self.bombimage3,(self.x+36,self.y+21))
		self.screen.blit(self.bombimage4,(self.x+36,self.y+21))

	def __del__(self):
		print("击落一架敌机")



def key_control(hero_temp):
	for event in pygame.event.get():
		if event.type == QUIT:
			print("exit()")
			exit()

	pressed_keys = pygame.key.get_pressed()
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		hero_temp.move_left()

	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		hero_temp.move_right()

	if pressed_keys[K_SPACE]:
		hero_temp.fire()

def main():
	screen=pygame.display.set_mode((512,568),0,0)
	background=pygame.image.load("./images/bg2.jpg")
	hero=Heroplane(screen)
	enemylist=[]
	m=-968
	h=0
	while True:
		h+=1
		screen.blit(background,(0,m))
		m+=2
		if m >= -200:
			m=-968
		hero.display()
		key_control(hero)
		if random.choice(range(50))==10:
			enemylist.append(EnemyPlane(screen))
		for em in enemylist:
			em.display()
			if h%50 == 0:
				em.fire()
			if em.move(hero):
				bombobje=bombimage(screen,em.x,em.y)
				enemylist.remove(em)
				bombobje.bombdisplay()
				del bombobje
			hero.check(em)
		pygame.display.update()
		time.sleep(0.04)



if __name__ == "__main__":
	main()


	
		
		

